package com.aliwo.design.patterns.simpleFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * @author xuyayuan
 * @description 读取自定义配置文件
 * @date 2021/9/8 10:10
 */
public class XmlUtil {
    /**
     * 功能描述:从xml配置文件中提取品牌名称，
     * @author xuyayuan
     * @param
     * @return java.lang.String
     */
    public static String getBrandName() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            org.w3c.dom.Document doc;
            doc =  builder.parse(new File("D:\\person-workSpace\\reflect-project\\src\\main\\resources" +
                                                  "\\simpleFactoryconfig.xml"));
            NodeList n1 = doc.getElementsByTagName("brandName");
            Node classNode = n1.item(0).getFirstChild();
            String brandName = classNode.getNodeValue().trim();
            return brandName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
